package cn.tedu.autoshow.passport.service.impl;

import cn.tedu.autoshow.commons.ex.ServiceException;
import cn.tedu.autoshow.commons.web.ServiceCode;
import cn.tedu.autoshow.passport.mapper.AdminMapper;
import cn.tedu.autoshow.passport.mapper.AdminRoleMapper;
import cn.tedu.autoshow.passport.mapper.RoleMapper;
import cn.tedu.autoshow.passport.pojo.entity.Admin;
import cn.tedu.autoshow.passport.pojo.entity.AdminRole;
import cn.tedu.autoshow.passport.pojo.entity.Role;
import cn.tedu.autoshow.passport.service.IAdminRoleService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.*;

@Service
@Slf4j
public class AdminRoleServiceImpl implements IAdminRoleService {

    @Autowired
    private AdminRoleMapper mapper;
    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public void insertsAdminRoleByAdminId(Long adminId, Long[] roleIds) {
        log.info("开始:【配置用户角色数据】");
        log.debug("传入参数,adminId={}", adminId);
        //检查用户ID检查用户是否存在数据，如果不存在，则抛出异常
        QueryWrapper<Admin> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("id", adminId);
        int countById = adminMapper.selectCount(queryWrapper2);
        log.info("规则：根据用户id检索数据库结果,countById={}", countById);
        if(countById == 0){
            log.info("结果2:根据用户id检索数据量为0，抛出异常...");
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, "删除用户失败，用户数据不存在！");
        }

        log.info("新增用户角色关系数据...");
        AdminRole[] adminRoles = new AdminRole[roleIds.length];
        LocalDateTime now = LocalDateTime.now();
        for (int i = 0; i < adminRoles.length; i++) {
            AdminRole adminRole = new AdminRole();
            adminRole.setAdminId(adminId).setRoleId(roleIds[i])
                    .setGmtCreate(now).setGmtModified(now);
            adminRoles[i] = adminRole;
        }
        log.debug("新增用户角色关系数据,adminRoles={}", adminRoles);
        int rows = mapper.insertBatch(adminRoles);
        if(rows != roleIds.length){
            String message = "新增用户角色关系失败，服务器忙，请稍后再试！";
            log.info("结果1:数据返回行数不满足预估行数，预估={}, 实际={}, 抛出自定异常...", roleIds.length, rows);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
        log.info("结果1:新增用户角色关系结果,rows={}", rows);
    }

    @Override
    public void insertsAdminRoleByRoleId(Long roleId, Long[] adminIds) {
        log.info("开始:【配置用户角色数据】");
        log.debug("传入参数,roleId={}", roleId);
        //检查用户ID检查用户是否存在数据，如果不存在，则抛出异常
        QueryWrapper<Role> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("id", roleId);
        int countById = roleMapper.selectCount(queryWrapper2);
        log.info("规则：根据角色id检索数据库结果,countById={}", countById);
        if(countById == 0){
            log.info("结果2:根据角色id检索数据量为0，抛出异常...");
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, "配置用户角色数据失败，角色数据不存在！");
        }

        log.info("新增用户角色关系数据...");
        AdminRole[] adminRoles = new AdminRole[adminIds.length];
        LocalDateTime now = LocalDateTime.now();
        for (int i = 0; i < adminRoles.length; i++) {
            AdminRole adminRole = new AdminRole();
            adminRole.setAdminId(adminIds[i]).setRoleId(roleId)
                    .setGmtCreate(now).setGmtModified(now);
            adminRoles[i] = adminRole;
        }
        log.debug("新增用户角色关系数据,adminRoles={}", adminRoles);
        int rows = mapper.insertBatch(adminRoles);
        if(rows != adminIds.length){
            String message = "新增用户角色关系失败，服务器忙，请稍后再试！";
            log.info("结果1:数据返回行数不满足预估行数，预估={}, 实际={}, 抛出自定异常...", adminIds.length, rows);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
        log.info("结果1:新增用户角色关系结果,rows={}", rows);
    }

    @Override
    public void deletesAdminRoleByAdminId(Long adminId, Long[] roleIds) {
        log.info("开始:【删除用户角色关系数据】");
        log.debug("传入参数,adminId={}", adminId);
        //检查用户ID检查用户是否存在数据，如果不存在，则抛出异常
        QueryWrapper<Admin> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("id", adminId);
        int countById = adminMapper.selectCount(queryWrapper2);
        log.info("规则：根据用户id检索数据库结果,countById={}", countById);
        if(countById == 0){
            log.info("结果2:根据用户id检索数据量为0，抛出异常...");
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, "删除用户角色关系失败，用户数据不存在！");
        }

        log.info("删除用户角色关系数据...");
        log.debug("删除用户角色关系数据,adminId={},roleIds={}", adminId, roleIds);
        int rows = mapper.deleteRoleByAdmin(adminId, roleIds);
        if(rows != roleIds.length){
            String message = "删除用户角色关系数据，服务器忙，请稍后再试！";
            log.info("结果1:数据返回行数不满足预估行数，预估={}, 实际={}, 抛出自定异常...", roleIds.length, rows);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
        log.info("结果1:删除用户角色关系结果,rows={}", rows);
    }

    @Override
    public void deletesAdminRoleByRoleId(Long roleId, Long[] adminIds) {
        log.info("开始:【删除用户角色关系数据】");
        log.debug("传入参数,roleId={}", roleId);
        //检查用户ID检查用户是否存在数据，如果不存在，则抛出异常
        QueryWrapper<Role> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("id", roleId);
        int countById = roleMapper.selectCount(queryWrapper2);
        log.info("规则：根据用户id检索数据库结果,countById={}", countById);
        if(countById == 0){
            log.info("结果2:根据用户id检索数据量为0，抛出异常...");
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, "删除用户角色关系失败，角色数据不存在！");
        }

        log.info("删除用户角色关系数据...");
        log.debug("删除用户角色关系数据,roleId={},adminIds={}", roleId, adminIds);
        int rows = mapper.deleteAdminByRole(roleId, adminIds);
        if(rows != adminIds.length){
            String message = "删除用户角色关系数据，服务器忙，请稍后再试！";
            log.info("结果1:数据返回行数不满足预估行数，预估={}, 实际={}, 抛出自定异常...", adminIds.length, rows);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
        log.info("结果1:删除用户角色关系结果,rows={}", rows);
    }

    @Override
    public List<Long> roleIdListByAdminId(Long adminId) {
        log.info("开始:【根据adminId查询用户有多少角色】");
        log.debug("传入参数,adminId={}", adminId);
        List<AdminRole> adminRoles = mapper.roleListByAdminId(adminId);
        List<Long> roleIds = new ArrayList<>();
        for (AdminRole adminRole : adminRoles) {
            roleIds.add(adminRole.getRoleId());
        }

        log.info("结果1:查询出数据roleIds={}", roleIds);
        return roleIds;
    }

    @Override
    public List<Long> adminIdListByRoleId(Long roleId) {
        log.info("开始:【根据roleId查询角色下有多少用户】");
        log.debug("传入参数,roleId={}", roleId);
        List<AdminRole> adminRoles = mapper.adminListByRoleId(roleId);
        List<Long> adminIds = new ArrayList<>();
        for (AdminRole adminRole : adminRoles) {
            adminIds.add(adminRole.getAdminId());
        }

        log.info("结果1:查询出数据adminIds={}", adminIds);
        return adminIds;
    }
}
